package com.attendance.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.attendance.dao.ApplyLeaveRepository;
import com.attendance.entity.ApplyLeave;
import com.attendance.entity.dto.AbsenceDTO;
import com.attendance.entity.dto.ApplyLeaveDTO;
import com.common.util.Examine;
import com.institute.dao.InstituteRepository;
import com.institute.service.IInstituteService;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;
import com.staff.service.IStaffService;

@Service
@Transactional
public class ApplyLeaveService implements IApplyLeaveService {
	@Autowired
	private ApplyLeaveRepository applyLeaveDao;
	@Autowired
	private StaffRepository staffDao;
	@Autowired
	private IInstituteService instituteService;

	public Page<ApplyLeaveDTO> findPage(String staffName, String staffNum, Pageable pageable) {
		List<ApplyLeaveDTO> results = new ArrayList<ApplyLeaveDTO>();
		List<ApplyLeave> list = new ArrayList<ApplyLeave>();
		if (staffName != "" && staffName != null) {
			list = applyLeaveDao.findLikeStaffName(staffName);
		} else if (staffNum != "" && staffNum != null) {
			list = applyLeaveDao.findLikeStaffNum(staffNum);
		} else {
			list = (List<ApplyLeave>) applyLeaveDao.findAll();
		}
		for (ApplyLeave a : list) {
			Staff staff = a.getStaff();
			String name = null;
			String num = null;
			String auditor = null;
			if (staff != null) {
				name = staff.getStaffName();
				num = staff.getStaffNum();
			}
			if (a.getAuditor() != null) {
				auditor = a.getAuditor().getStaffName();
			}
			results.add(new ApplyLeaveDTO(name, num, a, auditor));
		}
		List<ApplyLeaveDTO> page = new ArrayList<ApplyLeaveDTO>();
		int i = pageable.getPageNumber() * pageable.getPageSize();
		int n = i+pageable.getPageSize();
		while (i < results.size()&&i<n) {
			page.add(results.get(i));
			i++;
		}
		return new PageImpl<ApplyLeaveDTO>(page, pageable, page.size());
	}

	public Page<ApplyLeaveDTO> findPersonalApply(Long staffId, Pageable pageable) {
		List<ApplyLeave> list = applyLeaveDao.findByStaffId(staffId);
		List<ApplyLeaveDTO> results = new ArrayList<ApplyLeaveDTO>();
		Staff staff = staffDao.findOne(staffId);
		if(staff!=null) {	
			for(ApplyLeave a:list) {
				String auditor = null;
				if(a.getAuditor()!=null)auditor = a.getAuditor().getStaffName();
				results.add(new ApplyLeaveDTO(staff.getStaffName(), staff.getStaffNum(), a, auditor));
			}
		}
		List<ApplyLeaveDTO> page = new ArrayList<ApplyLeaveDTO>();
		int i = pageable.getPageNumber()*pageable.getPageSize();
		int n = i+pageable.getPageSize();
		while(i<results.size()&&i<n) {
			page.add(results.get(i));
			i++;
		}
		return new PageImpl<ApplyLeaveDTO>(page, pageable, results.size()); 
	}
	//审核人(部门负责人)查找待审核申请
	public Page<ApplyLeaveDTO> findUnauditedApply(Long auditorId,Pageable pageable) {
		Staff staff = staffDao.findOne(auditorId);
		if(!instituteService.isPrincipal(auditorId)||staff==null)return null;
		if(staff.getDepartment()==null)return null;
		List<ApplyLeaveDTO> results = new ArrayList<ApplyLeaveDTO>();
		List<ApplyLeave> list = (List<ApplyLeave>) applyLeaveDao.findByInstitute(staff.getDepartment().getId());
		for(ApplyLeave a:list) {
			if(a.getStatus() == Examine.Unaudited) {
				Staff s = a.getStaff();
				String auditor = null;
				if(a.getAuditor()!=null)auditor = a.getAuditor().getStaffName();
				results.add(new ApplyLeaveDTO(s.getStaffName(),s.getStaffNum(),a,auditor));
			}
		}
		List<ApplyLeaveDTO> page = new ArrayList<ApplyLeaveDTO>();
		int i = pageable.getPageNumber()*pageable.getPageSize();
		int n = i+pageable.getPageSize();
		while(i<results.size()&&i<n) {
			page.add(results.get(i));
			i++;
		}
		return new PageImpl<ApplyLeaveDTO>(page, pageable, results.size()); 
	}

	public boolean isLauncher(Long launchId, Long applyLeaveId) {
		ApplyLeave applyLeave = applyLeaveDao.findOne(applyLeaveId);
		if(applyLeave!=null) {
			if(applyLeave.getStaff()!=null) {
				if(applyLeave.getStaff().getId()==launchId)return true;
			}
		}
		return false;
	}

	public void saveOrUpdate(ApplyLeave applyLeave,Staff staff) {
		if(applyLeave.getId()==null) {
			//新建
			
			Staff s = staffDao.findOne(staff.getId());
			applyLeave.setLaunchTime(new Date());
			applyLeave.setStaff(s);
			applyLeave.setStatus(Examine.Unaudited);
			applyLeaveDao.save(applyLeave);
		}else {
			//更新
			ApplyLeave a = applyLeaveDao.findOne(applyLeave.getId());
			a.setReason(applyLeave.getReason());
			a.setStartTime(applyLeave.getStartTime());
			a.setEndTime(applyLeave.getEndTime());
			a.setStatus(Examine.Unaudited);
			applyLeaveDao.save(a);
		}
	}

	public ApplyLeave findOne(Long id) {
		return applyLeaveDao.findOne(id);
	}

	public void toExamine(ApplyLeave applyLeave, Examine status, Long auditorId) {
		ApplyLeave al = applyLeaveDao.findOne(applyLeave.getId());
		Staff staff = staffDao.findOne(auditorId);
		if(al!=null&&staff!=null&&(status == Examine.Pass||status == Examine.NotPass)) {
			if(al.getStatus() == Examine.Unaudited) {
				al.setExamineReason(applyLeave.getExamineReason());
				al.setStatus(status);
				al.setExamineTime(new Date());
				al.setAuditor(staff);
				applyLeaveDao.save(al);
			}
		}
	}

	public void callBack(Long applyLeaveId, Long staffId) {
		ApplyLeave applyLeave = applyLeaveDao.findOne(applyLeaveId);
		if(applyLeave.getStaff().getId()==staffId) {
			applyLeave.setStatus(Examine.Retracted);
		}
	}

	public Staff getStaff(Long id) {
		ApplyLeave applyLeave = applyLeaveDao.findOne(id);
		if(applyLeave == null)return null;
		return applyLeave.getStaff();
	}
	
}
